<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="店铺ID" prop="organId">
        <el-input
          v-model="queryParams.organId"
          placeholder="请输入店铺ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="workshopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="店铺ID" align="center" prop="organId">
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="店铺设备数" align="center" prop="shopDeviceNum" />
      <el-table-column
        label="店铺人员数"
        align="center"
        prop="shopPersonnelNum"
      />
      <el-table-column label="店铺状态" align="center" prop="runStatus">
        <template slot-scope="scope">
          <span
            v-if="scope.row.runStatus == '0'"
            style="color: green; font-weight: bold"
            >正常</span
          >
          <span
            v-if="scope.row.runStatus == '1'"
            style="color: red; font-weight: bold"
            >停运</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workshop:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="configuration(scope.row)"
            v-hasPermi="['mes:md:workshop:remove']"
            >配置</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workshop:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑店铺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="店铺ID" prop="organId">
              <el-input v-model="form.organId" placeholder="请输入店铺ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="店铺名称" prop="shopName">
              <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="form.projectAddress" label="店铺地址:">
              <div style="width: 100%">
                <el-select
                  v-model="form.province"
                  placeholder="请选择省"
                  style="width: 20%"
                  @change="getSubRegionByRegionCode('cityArray', $event)"
                >
                  <el-option
                    v-for="item in provinceArray"
                    :key="item.regionCode"
                    :label="item.regionName"
                    :value="item.regionCode"
                  />
                </el-select>
                <el-select
                  v-model="form.city"
                  placeholder="请选择市"
                  style="width: 20%"
                  @change="getSubRegionByRegionCode('areaArray', $event)"
                >
                  <el-option
                    v-for="item in cityArray"
                    :key="item.regionCode"
                    :label="item.regionName"
                    :value="item.regionCode"
                  />
                </el-select>
                <el-select
                  v-model="form.county"
                  style="width: 20%"
                  placeholder="请选择区"
                >
                  <el-option
                    v-for="item in areaArray"
                    :key="item.regionCode"
                    :label="item.regionName"
                    :value="item.regionCode"
                  />
                </el-select>
                <el-input
                  style="width: 40%"
                  v-model="form.projectAddress"
                  placeholder="请录入详细地址"
                />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkMan">
              <el-input v-model="form.linkMan" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkPhone">
              <el-input v-model="form.linkPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="营业时间" prop="charge">
              <div style="display: flex">
                <el-select
                  v-model="form.startDate"
                  clearable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in optionsDate"
                    :key="item.value"
                    :label="item.label"
                    :value="item.label"
                  >
                  </el-option>
                </el-select>
                <el-select
                  v-model="form.endDate"
                  clearable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in optionsDate"
                    :key="item.value"
                    :label="item.label"
                    :value="item.label"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="charge">
              <el-time-picker
                is-range
                v-model="form.contactTime"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                value-format="HH:mm:ss"
                @change="changeFormat"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="5">
          <el-form-item label="是否全天" prop="whetherAllday">
            <el-switch v-model="delivery"></el-switch>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAcademy,
  insertShopInfo,
  updateShopInfo,
  deleteShopInfo,
} from "@/api/shop/storeList.js";
import * as dictServer from "@/api/shop/dict.js"; // Secondary package based on el-pagination

export default {
  name: "Workshop",
  dicts: ["sys_yes_no"],
  data() {
    return {
      autoGenFlag: true,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      workshopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      delivery: false,
      provinceArray: [],
      cityArray: [],
      areaArray: [],
      optionsDate: [
        {
          value: "1",
          label: "周一",
        },
        {
          value: "2",
          label: "周二",
        },
        {
          value: "3",
          label: "周三",
        },
        {
          value: "4",
          label: "周四",
        },
        {
          value: "5",
          label: "周五",
        },
        {
          value: "6",
          label: "周六",
        },
        {
          value: "7",
          label: "周天",
        },
      ],
      // 表单校验
      rules: {
        workshopCode: [
          { required: true, message: "编码不能为空", trigger: "blur" },
        ],
        workshopName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getProvince();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAcademy(this.queryParams).then((response) => {
        this.workshopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organId: "",
        shopName: "",
        shopLocation: "",
        runStatus: "",
        linkMan: "",
        linkPhone: "",
        startDate: "",
        endDate: "",
        startTime: "",
        endTime: "",
        whetherAllday: "",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("form");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.workshopId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建店铺";
      this.optType = "add";
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      // 获取地区信息
      if (this.form.province) {
        this.getRegionCode("cityArray", this.form.province);
      }
      if (this.form.city) {
        this.getRegionCode("areaArray", this.form.city);
      }
      if (row.whetherAllday != 0) {
        this.delivery = true;
      } else {
        this.delivery = false;
      }
      if (this.form.startTime != "" || this.form.endTime != "") {
        var arr = [];
        arr.push(this.form.startTime);
        arr.push(this.form.endTime);
        this.form.contactTime = arr;
      }
      this.open = true;
      this.title = "编辑店铺";
      this.optType = "edit";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if (this.delivery != true) {
              this.form.whetherAllday = 0;
            } else {
              this.form.whetherAllday = 1;
            }
            updateShopInfo(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.delivery != true) {
              this.form.whetherAllday = 0;
            } else {
              this.form.whetherAllday = 1;
            }
            insertShopInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workshopIds = row.id || this.id;
      deleteShopInfo({ id: workshopIds }).then((response) => {
        this.$modal
          .confirm('是否确认删除店铺名称为"' + row.shopName + '"的数据项？')
          .then(() => {
            this.$modal.msgSuccess("删除成功");
            this.open = false;
            this.getList();
          })
          .catch(() => {});
      });
    },
    configuration(row) {
      // 存储数据
      localStorage.setItem("organId", row.organId);
      this.$router.push({
        path: "/shop/configuration",
        query: {
          id: row.organId,
        },
      });
    },
    getProvince() {
      dictServer.getFirstLevel().then((res) => {
        this.provinceArray = res.result;
      });
    },
    getSubRegionByRegionCode(type, regionCode) {
      this.getRegionCode(type, regionCode);
    },
    getRegionCode(type, regionCode) {
      dictServer.getSubRegionByRegionCode(regionCode).then((res) => {
        this[type] = res.result;
      });
    },
    changeFormat(e) {
      this.form.startTime = e[0];
      this.form.endTime = e[1];
    },
  },
};
</script>
